import { parseTime } from '@/utils/index'

export function notifySuccess(obj, message) {
  obj.$notify({
    title: message,
    message: message,
    type: 'success',
    duration: 4000
  })
}
export function my_confirm(obj, callback) {
  obj.$confirm('此操作将删除该信息, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    callback()
  }).catch(() => {
    this.$message({
      type: 'info',
      message: '已取消删除'
    })
  })
}

export function myConfirmMessage(message, obj, callback) {
  obj.$confirm(message, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    callback()
  }).catch(() => {
    this.$message({
      type: 'info',
      message: '已取消删除'
    })
  })
}

export function formatJson(obj, filterVal) {
  return obj.list.map(v => filterVal.map(j => {
    if (j === 'timestamp') {
      return parseTime(v[j])
    } else {
      // eslint-disable-next-line eqeqeq
      if (j == 'is_show' || j == 'is_del') {
        // eslint-disable-next-line eqeqeq
        v[j] = v[j] == 0 ? '否' : '是'
        return v[j]
      } else if (j.indexOf('.') > -1) {
        const json = j.split('.')
        return JSON.parse(v[json[0]])[json[1]]
      } else {
        return v[j]
      }
    }
  }))
}

/**
 * 搜索的时候参数过滤
 * @param listQuery
 * @returns {*}
 */
export function listQueryFilter(listQuery) {
  for (var key in listQuery) {
    if (listQuery[key] === '' || listQuery[key] === undefined) {
      delete listQuery[key]
    }
  }
  return listQuery
}

/**
 * 获取文件地址
 * @param url
 * @returns {string}
 */
export function getFileUrl(url) {
  return process.env.VUE_APP_BASE_API + url
}

/**
 * 分钟时间转换成小时字符串
 * @param num
 * @returns {string}
 */
export function minuteToStr(num) {
  var hour = Math.floor(num / 60)
  var minut = num - hour * 60
  if (hour > 0) {
    return hour + '小时' + minut + '分'
  } else {
    return minut + '分'
  }
}

